package org.me.Anime.Adapter;

import java.util.List;

import org.me.Anime.DrawAnimeActivity;
import org.me.Anime.DrawAnimeImageActivity;
import org.me.Anime.R;
import org.me.Anime.Data.Image;
import org.me.Anime.Utli.FileUtli;
import org.me.Anime.Utli.GlobalVariable;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ListImageAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
    private Context mContext;
    private List<Image> mListImage = null;
    String filename;
    String folder;
    private Bitmap mBitmap = null;
    private BitmapDrawable bitmapDrawable;
    
    public ListImageAdapter(Context context,List<Image> _mListImage, String _filename)
    {
    	mContext  = context;
    	mListImage   = _mListImage;   
    	filename = _filename;
    	mInflater = LayoutInflater.from(context);				
    }
    
    public class ViewHolder
	{
		ImageView image;
	}
    
	@Override
	public View getView(final int position, View view, ViewGroup parent)
	{
		final ViewHolder holder;
		if (view == null)
		{
			holder 					= new ViewHolder();
			view 					= mInflater.inflate(R.layout.item_enum_image, null);
			holder.image		= (ImageView)view.findViewById(R.id.imageView);
			view.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) view.getTag();
		}
		
		holder.image.setTag(position);
		folder = filename+"/"+mListImage.get(position).GetSrc();
		Log.e("getView", "folder = "+folder);
		GlobalVariable.byteBuffer = FileUtli.readFileFromAssets(mContext, folder);
		mBitmap	= BitmapFactory.decodeByteArray(GlobalVariable.byteBuffer, 0, GlobalVariable.byteBuffer.length);
		bitmapDrawable = new BitmapDrawable(mBitmap);
		holder.image.setBackgroundDrawable(bitmapDrawable);
		holder.image.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				GlobalVariable.fileName = filename+"/"+mListImage.get(position).GetSrc();
				GlobalVariable.checkPickColor = false;
				((DrawAnimeImageActivity)mContext).finish();
				
				
//				Intent intent = new Intent(mContext, DrawAnimeActivity.class);
//				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//				mContext.startActivity(intent);
				
			}
		});
		GlobalVariable.byteBuffer = null;
		return view;		
	}
	
	

	@Override
	public int getCount() {
		return mListImage.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
